package WaitNotify;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-12-25
 * Time: 10:12
 */
public class TestDemo4 {
    private static final Object obj = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            System.out.println("t1线程执行");
            synchronized (obj) {
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t1线程执行其他代码");
        });
        t1.start();
        Thread t2 = new Thread(()->{
            System.out.println("t2线程执行");
            synchronized (obj) {
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t2线程执行其他代码");
        });
        t2.start();
        Thread.sleep(2000);
        //主线程执行
        synchronized (obj){
            obj.notifyAll();//主线程随机唤醒一个线程
        }
    }
}
